package com.moon.interface_api.dataSource;

import com.moon.interface_api.enums.DataSourceKey;
import com.moon.interface_api.interceptor.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

@Slf4j
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
    
    /**
     *@Author:chengliang
     *@date 2018/12/12_10:06
     *@param 
     *@return 
     * 这个方法是获取map中我们需要的key,传入key，我们就能获取到key对应的数据源
     */
    @Override
    protected Object determineCurrentLookupKey() {
        DataSourceKey dataSourceKey = DynamicDataSourceContextHolder.get();
        if(dataSourceKey == null){
            log.info("当前数据源为默认数据源：master");
        }else {
            log.info("当前数据源为："+dataSourceKey);
        }
        return dataSourceKey;
    }
}
